package com.yidu.qianfeng.controller;

import com.yidu.qianfeng.common.bean.Vehicle.CarInfo;
import com.yidu.qianfeng.common.bean.company.EmpInfo;
import com.yidu.qianfeng.common.bean.company.Subst;
import com.yidu.qianfeng.common.bean.shunt.ShuntLeftBean;
import com.yidu.qianfeng.common.bean.transport.TransportProtocol;
import com.yidu.qianfeng.common.bean.waybill.Waybill;
import com.yidu.qianfeng.service.ShuntServiceInter;
import com.yidu.qianfeng.service.TrackWaybillService;
import com.yidu.qianfeng.service.impl.FinanceApi;
import com.yidu.qianfeng.service.impl.ShuntServiceImpl;
import com.yidu.qianfeng.service.impl.TrackWaybillServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 分流
 * @author 张兴兴
 * @date 2021/3/25 15:35
 */
@Controller("shuntController")
public class ShuntController {

    @Resource
    private ShuntServiceInter shuntServiceInter;
    private ShuntServiceImpl shuntServiceImpl;

    @Resource
    private TrackWaybillService trackWaybillService;
    @Resource
    private TrackWaybillServiceImpl trackWaybillServiceImpl;


    //注入财务api
    @Resource(name = "financeApi")
    private FinanceApi financeApi;

    /**
     * 查询 卸货入库
     * @param carloadTransNum 装车表(装车运输单号)
     * @return
     */
    @RequestMapping("/queryTrans")
    @ResponseBody
    public Map<String, Object> getShuntAll(String carloadTransNum, HttpServletRequest request) {
        List<TransportProtocol> list = shuntServiceInter.queryTrans(carloadTransNum, UserController.getLoginEmpInfo(request).getEmpSubstId());

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    @RequestMapping("/queryWaybill")
    @ResponseBody
    public Map<String, Object> getShuntWay(String number) {
        //声明要返回的map集合
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");

        if (number != null || number != " ") {
            List<Waybill> list = new ArrayList<Waybill>();
            //根据 前台返回的装车序号 查询返回 出运单表的图运单号
            List<String> seqNum = shuntServiceInter.queryWaybillConsigNum(number);
            if (seqNum != null) {
                //将托运单号进行切割
                Object[] consignNumArray = seqNum.toArray();
                //循环数组获取运单
                for (int i = 0; i < consignNumArray.length; i++) {
                    //将运单传递 查询此运单的信息
                    Waybill waybill = shuntServiceInter.queryWaybill((String) consignNumArray[i], 4);
                    //判断是否已经卸货  如果已经卸货则不添加到集合中
                    Waybill existWay = shuntServiceInter.queryWaybill((String) consignNumArray[i], 5);
                    //添加到集合
                    if (waybill != null && existWay == null)
                        list.add(waybill);
                }
            }
            map.put("count", list.size());
            map.put("data", list);
        } else {
            map.put("count", null);
            map.put("data", null);
        }  return map;

    }

    /**
     *
     * @param number 运输装车序号
     * @param num   运单号
     * @param request 当前登录用户
     * @return
     */
    @RequestMapping("/queryWaybillBottom")
    @ResponseBody
    public Map<String, Object> getShuntBottomWay(String number, String num, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Waybill> list = new ArrayList<Waybill>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", null);
        map.put("data", null);
        //判断 运输装车序号 是否为空
        if (number != null) {
            //根据 运输装车序号 查询出 托运单号
            List<String> seqNum = shuntServiceInter.queryWaybillConsigNum(number);
            if (seqNum != null) {
                //将查出来得 托运单号 转成 数组
                Object[] consignNumArray = seqNum.toArray();
                //判断 运单号 是否为空
                if (num != null) {
                    //将 运单号 进行切割
                    String[] split = num.split(",");
                    //循环 运单 数组
                    for (int i = 0; i < split.length; i++) {
                        //声明 运单信息实体 对象
                        Waybill bill = new Waybill();
                        //给运单实体对象的 托运单号属性设置值
                        bill.setWaybillConsignNum(split[i]);
                        //根据 当前登录的用户分站id  查询出分站对象
                        Subst subst = shuntServiceInter.selectSubst(UserController.getLoginEmpInfo(request).getEmpSubstId());
                        //新增跟踪
                        trackWaybillService.generateTrackShunt(bill, subst, UserController.getLoginEmpInfo(request));
                        //清空 运单实体对象的 托运单号属性的值
                        bill.setWaybillConsignNum("");
                    }
                }
                //清空map集合
                map.clear();
                //循环   托运单号 数组
                for (int i = 0; i < consignNumArray.length; i++) {
                    //根据 托运单号 查询在途的运单
                    Waybill waybill = shuntServiceInter.queryWaybill((String) consignNumArray[i], 5);
                    //返回值 是否为空
                    if (waybill != null) {
                        //添加到list集合中
                        list.add(waybill);
                    }
                }
                map.put("code", 0);
                map.put("msg", "");
                map.put("count", list.size());
                map.put("data", list);
            }
        }
        return map;
    }

    /**
     * 分流
     * @param number  运单编号
     * @param request  当前登录用户
     * @return
     */
    @RequestMapping("/queryShunt")
    @ResponseBody
    public Map<String, Object> queryShunt(String number , HttpServletRequest request) {
        //声明要返回的集合
        Map<String, Object> map = new HashMap<String, Object>();
        List<Waybill> list = shuntServiceInter.queryShunt(number, 5,UserController.getLoginEmpInfo(request).getEmpSubstId());
        List<Waybill> way = shuntServiceInter.queryShunt(number, 6,UserController.getLoginEmpInfo(request).getEmpSubstId());
        for (Waybill wayb : way) {
            for (Waybill waybill : list) {
                //判断 运单信息实体 的 运单跟踪实体 的属性的运单 跟踪表托运单号 是否一致
                if (waybill.getTrackWaybill().getTrackConsignNum().equals(wayb.getTrackWaybill().getTrackConsignNum())) {
                    //将list中的waybill对象 移除
                    list.remove(waybill);
                    //结束循环
                    break;
                }
            }
        }
        //layui数据表格格式
        map.put("code", 0);
        map.put("msg", "");
        if (list != null) {
            map.put("count", list.size());
            map.put("data", list);
        } else {
            map.put("count", null);
            map.put("data", null);
        }
        return map;
    }

    @RequestMapping("/querySubs")
    @ResponseBody
    public Map<String, String> getSubsName(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("loginName", UserController.getLoginEmpInfo(request).getEmpName());
        String empSubstId = UserController.getLoginEmpInfo(request).getEmpSubstId();
        map.put("loginSubsName", shuntServiceInter.selectSubst(empSubstId).getSubstName());
        map.put("nowDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        map.put("deliveryId", new Random().nextInt(10000) + "" + new Random().nextInt(10000) + "" + new Random().nextInt(10));
        return map;
    }

    @RequestMapping("/queryDelivery")
    @ResponseBody
    public Map<String, Object> loadDelivery(String number) {
        //声明waybill对象 list集合
        List<Waybill> list = new ArrayList<>();
        //声明返回的集合类型
        Map<String, Object> map = new HashMap<String, Object>();
        if (number != null) {
            String[] split = number.split(",");
            //循环数组
            for (int i = 0; i < split.length; i++) {
                //根据数组中的值 查询 要分流派送 的运单
                Waybill waybill = shuntServiceInter.queryDelivery(split[i]);
                //查询分流对象判断是否为空
                if (waybill != null) {
                    //添加到list集合
                    list.add(waybill);
                }
            }
        }
        //      //layui数据表格格式
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }


    @RequestMapping("/queryCarInfo")
    @ResponseBody
    public CarInfo queryCarInfo(String name) {
        CarInfo querydriverinfo = new CarInfo();
        if (name != null) {
            querydriverinfo = shuntServiceInter.querydriverinfo(name);
        }
        return querydriverinfo;
    }

    @RequestMapping("/queryCarInfoAll")
    @ResponseBody
    public List<CarInfo> queryCarInfoAll() {
        List<CarInfo> list = shuntServiceInter.querydriverinfoAll();
        return list;
    }


    @RequestMapping("/deliveryUpType")
    @ResponseBody
    public Map<String, Object> deliveryUpdateType(String deliveryId, String number, String carNumber, double money,String phone, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        HashMap<String, Object> addMap = new HashMap<String, Object>();
        addMap.put("deliveryId", deliveryId);
        addMap.put("carNumber", carNumber);
        addMap.put("money", money);
        addMap.put("number", number);
        addMap.put("phone", phone);
        if (number != null) {
            String[] split = number.split(",");
            for (int i = 0; i < split.length; i++) {
                Waybill bill = new Waybill();
                bill.setWaybillConsignNum(split[i]);
                Subst subst = shuntServiceInter.selectSubst(UserController.getLoginEmpInfo(request).getEmpSubstId());
                CarInfo carInfo = shuntServiceInter.queryDriverT(carNumber);
                //新增跟踪
                trackWaybillServiceImpl.generateTrackDelivery(bill, carInfo, UserController.getLoginEmpInfo(request));
                bill.setWaybillConsignNum("");
            }
            int addDelivery = shuntServiceInter.addDelivery(addMap);
        }
        //生成财务记录
        //支出
        financeApi.financeRecord(0,
                "派送开支",
                money,
                "派送费现付",
                "派送编号："+deliveryId,
                shuntServiceInter.queryDriverT(carNumber).getCarDriver(),
                "株洲站",
                UserController.getLoginEmpInfo(request));
        return map;
    }

    @RequestMapping("/deList")
    @ResponseBody
    public Map<String, Object> deList(String id,String carName) {
//        System.out.println(id+"id");
        Map<String, Object> map = new HashMap<String, Object>();
        List<ShuntLeftBean> list = shuntServiceInter.queryDeliveryList(id,carName);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    @RequestMapping("/sendDelivery")
    @ResponseBody
    public Map<String, Object> sendDelivery(Integer sendNum) {
        List<Waybill> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<String, Object>();
        if (sendNum != null) {
            String waybillNumber = shuntServiceInter.queryDeliveryWaybill(sendNum);
            if (waybillNumber != null) {
                String[] split = waybillNumber.split(",");
                for (int i = 0; i < split.length; i++) {
                    Waybill waybill = shuntServiceInter.queryDelivery(split[i]);
                    if (waybill != null) {
                        list.add(waybill);
                    }
                }
            }
        }
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    @RequestMapping("/sendDeliveryTips")
    @ResponseBody
    public Map<String, Object> sendDeliveryTips(Integer sendNum,HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("loginName", UserController.getLoginEmpInfo(request).getEmpName());
        String empSubstId = UserController.getLoginEmpInfo(request).getEmpSubstId();
        map.put("loginSubsName", shuntServiceInter.selectSubst(empSubstId).getSubstName());
        ShuntLeftBean shuntLeftBean = shuntServiceInter.queryDeliveryTips(sendNum);
        map.put("bean",shuntLeftBean);
        return map;
    }

    @RequestMapping("/updateDelivery")
    @ResponseBody
    public String updateDelivery(Integer deliveryId, String number, String carNumber, double money, HttpServletRequest request) {
        String waybillNumber = shuntServiceInter.queryDeliveryWaybill(deliveryId);
        String[] waybillNumbers = waybillNumber.split(",");
        String[] numbers = number.split(",");
        List<String> other =new ArrayList<String>();
        for (int i = 0; i < waybillNumbers.length; i++) {
            boolean result =true;
            in:for (int j = 0; j < numbers.length; j++) {
                if (waybillNumbers[i].equals(numbers[j])) {
                    result=false;
                    break in;
                }
            }
            if (result==true) {
                other.add(waybillNumbers[i]);
            }
        }
        for (String o : other) {
            shuntServiceInter.deleteDriver(o);
        }

        HashMap<String, Object> addMap = new HashMap<String, Object>();
        addMap.put("deliveryId", deliveryId);
        addMap.put("carNumber", carNumber);
        addMap.put("money", money);
        /*运单编号*/
        addMap.put("number", number);
        int i = shuntServiceInter.updateDelivery(addMap);

        return "";
    }
}
